package com.zwk.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zwk.base.entity.CommonResult;
import com.zwk.base.util.JwtUtils;
import com.zwk.order.entity.Order;
import com.zwk.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author zwk
 * @since 2022-09-01
 */
@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 根据课程id, 用户id生成订单
    @PostMapping("/createOrder/{id}")
    public CommonResult saveOrder(@PathVariable("id") String courseId, HttpServletRequest request){
        // 返回订单号
        String orderNo = orderService.createOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return CommonResult.ok().data("orderId", orderNo);
    }

    // 根据订单号查询订单信息
    @GetMapping("/getOrderInfo/{id}")
    public CommonResult getOrderInfo(@PathVariable("id") String id){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", id);
        Order order = orderService.getOne(wrapper);
        return CommonResult.ok().data("item", order);
    }

    // 根据课程id和用户id查询用户是否购买过该课程
    @GetMapping("/isBuyCourse/{courseId}")
    public CommonResult isBuyCourse(@PathVariable("courseId") String courseId,
                               HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        boolean result = orderService.isBuyCourse(courseId, memberIdByJwtToken);
        return CommonResult.ok().data("isBuy", result);
    }

}

